const newMessageModel = require("./schema/newMessageSchema");
const blogModel = require("./schema/blogSchema");
//获取层主评论列表,参数文章id
module.exports.getCommentList = async (articleId) => {
  return await newMessageModel.UserComment.find({ articleId: articleId });
};

//新增留言
module.exports.addUserComment = async (articleId, commentInfo) => {
  const result = await newMessageModel.UserComment.find({ articleId });
  if (result.length == 0) {
    let newUserComment = new newMessageModel.UserComment({
      itemArticleComment: [],
      articleId,
    });
    newUserComment.itemArticleComment.push(commentInfo);
    newUserComment.save();
  } else {
    //如果有新的评论则更新文档
    //获取原先的评论数据
    let olditemArticleComment = result[0].itemArticleComment;
    olditemArticleComment.push(commentInfo);
    let updatacom = await newMessageModel.UserComment.updateOne(
      { articleId },
      { itemArticleComment: olditemArticleComment }
    );
    return updatacom;
  }
};

//新增回复
module.exports.addOtherComment = async (
  articleId,
  itemArticleCommentId,
  commentInfo
) => {
  //当前楼层有多少回复量
  let commentNumberT;
  const result = await newMessageModel.OtherComment.find({
    ceID: itemArticleCommentId,
  });
  //新增楼层中的评论信息
  if (result.length == 0) {
    let newotherComment = new newMessageModel.OtherComment({
      articleId,
      ceID: itemArticleCommentId,
      replyCon: [],
    });
    await newotherComment.save();
  }
  const result2 = await newMessageModel.OtherComment.findOne({
    ceID: itemArticleCommentId,
  });
  commentNumberT = result2.replyCon.length;
  let oldreplyinfo = result2.replyCon;
  oldreplyinfo.push(commentInfo);
  await newMessageModel.OtherComment.updateOne(
    { ceID: itemArticleCommentId },
    { replyCon: oldreplyinfo }
  );
  //更新原楼层里的回复数量
  const result3 = await newMessageModel.UserComment.findOne({ articleId });
  let data = result3.itemArticleComment;
  //定义一个变量用来接收需要改变的楼层值
  let needChange = {};
  //需要改变值的索引
  let needIndex;
  let newData = data.filter((currentValue, index) => {
    if (currentValue["_id"] == itemArticleCommentId) {
      needChange = currentValue;
      needIndex = index;
    }
    return currentValue["_id"] != itemArticleCommentId;
  });

  needChange.commentNumber = commentNumberT + 1;
  newData.splice(needIndex, 0, needChange);
  let addnumber = await newMessageModel.UserComment.updateOne(
    { articleId },
    { itemArticleComment: newData }
  );
  return addnumber;
};
//
module.exports.getOtherCommentList = async (ceId) => {
  const result = await newMessageModel.OtherComment.find({ ceID: ceId });
  return result;
};
module.exports.getAdminIsNoComList = async () => {
  const result = await blogModel.find({ adminNoCommentNumber: { $gte: 1 } });
  return result;
};

//修改层评论中的adminisComment属性
module.exports.changeAdminisComment = (articleId, ceID) => {
  return newMessageModel.UserComment.findOne({ articleId }).then((data) => {
    let list = data.itemArticleComment;
    //获取到符合条件的元素
    let newlist = list.map(function (item) {
      if (item._id == ceID) {
        item.adminisComment = true;
      }
      return item;
    });
    return newMessageModel.UserComment.updateOne(
      { articleId: articleId },
      { itemArticleComment: newlist }
    );
  });
};

//在管理员回复层主后如果有其他人回复则添加一条记录用作保存
module.exports.addReplyAdmin = (info) => {
  return new newMessageModel.AdminComment(info).save();
};

//删除一条记录
module.exports.delReplayAdminById = (id) => {
  return newMessageModel.AdminComment.deleteOne({ _id: id });
};
//获取层主中未回复的评论
module.exports.getNocomInfo = (articleId) => {
  return newMessageModel.UserComment.findOne({ articleId }).then((data) => {
    let list = data.itemArticleComment;
    //将列表中的 adminisComment 属性为 false 的过滤处理
    let newList = list.filter(function (item) {
      return item.adminisComment == false;
    });
    return newList;
  });
};
//在其他用户回复管理员的集合中获取未回复的数据
module.exports.getOtherNoCom = (articleId) => {
  let result = newMessageModel.AdminComment.find({ articleId });
  return result;
};
//删除博客连同博客的评论一起删除
module.exports.deleteBlogsMessage = async (articleId) => {
  await newMessageModel.UserComment.deleteMany({ articleId });
  await newMessageModel.OtherComment.deleteMany({ articleId });
  await newMessageModel.OtherComment.deleteMany({ articleId });
};
// 根据id 删除一条回复
module.exports.deleteOneReply = async (articleId, ceID, id) => {
  //当前楼层有多少回复量
  let commentNumberT;
  //获取到回复的楼层
  const result = await newMessageModel.OtherComment.findOne({
    articleId,
    ceID,
  });
  //获取到回复楼层中的回复
  let replyCon = result.replyCon;
  commentNumberT = replyCon.length;
  //删除replyCon中的id 为指定值的一项
  replyCon = replyCon.filter((item) => {
    return item.id !== id;
  });
  await newMessageModel.OtherComment.updateOne(
    { articleId, ceID },
    { replyCon }
  );
  //更新原楼层里的回复数量
  const result3 = await newMessageModel.UserComment.findOne({ articleId });
  let data = result3.itemArticleComment;
  //定义一个变量用来接收需要改变的楼层值
  let needChange = {};
  //需要改变值的索引
  let needIndex;
  let newData = data.filter((currentValue, index) => {
    if (currentValue["_id"] == ceID) {
      needChange = currentValue;
      needIndex = index;
    }
    return currentValue["_id"] != ceID;
  });

  needChange.commentNumber = commentNumberT - 1;
  newData.splice(needIndex, 0, needChange);
  await newMessageModel.UserComment.updateOne(
    { articleId },
    { itemArticleComment: newData }
  );
};
//分页获取博客楼层留了言
module.exports.deleteBlogsMessage;
